<template>
  <div class="content-wrapper">
    <div class="top-search-box">
      <!-- 高级查询 -->
      <a-form v-bind="layout" ref="formRef" :model="queryForm">
        <a-row>
          <a-col v-bind="colLayout.cols">
            <a-form-item label="流程名称">
              <a-input v-model:value="queryForm.title" placeholder="请输入流程名称" allow-clear @keyup.enter="handleQuery" />
            </a-form-item>
          </a-col>
          <a-col v-bind="colLayout.cols">
            <a-form-item label="流程状态">
              <a-select
                v-model:value="queryForm.businessState"
                :get-popup-container="(triggerNode) => triggerNode.parentNode"
                option-filter-prop="children"
                show-search
                allow-clear
                placeholder="请选择流程状态"
              >
                <a-select-option v-for="item in businessStateList" :key="item.sysLookupTlId" :value="item.lookupCode">
                  {{ item.lookupName }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col v-bind="colLayout.cols">
            <a-form-item label="创建时间(起)">
              <a-date-picker
                v-model:value="queryForm.startDateBegin"
                format="YYYY-MM-DD"
                value-format="YYYY-MM-DD"
                placeholder="请选择创建时间(起)"
                :disabled-date="(startValue) => proxy.$disabledStartDate(startValue, queryForm.startDateEnd)"
              />
            </a-form-item>
          </a-col>
          <a-col v-show="advanced" v-bind="colLayout.cols">
            <a-form-item label="创建时间(止)">
              <a-date-picker
                v-model:value="queryForm.startDateEnd"
                format="YYYY-MM-DD"
                value-format="YYYY-MM-DD"
                placeholder="请选择创建时间(止)"
                :disabled-date="(startValue) => proxy.$disabledEndDate(startValue, queryForm.startDateBegin)"
              />
            </a-form-item>
          </a-col>
          <a-col v-bind="colLayout.cols" style="margin-left: auto">
            <div class="table-page-search-submitButtons">
              <a-space>
                <a-button type="primary" @click="handleQuery">
                  <search-outlined />
                  查询
                </a-button>
                <a-button type="primary" ghost @click="resetQuery">
                  <redo-outlined />
                  清空
                </a-button>
                <a-button type="link" style="margin: 0" @click="toggleAdvanced">
                  {{ advanced ? '收起' : '展开' }}
                  <up-outlined v-if="advanced" />
                  <down-outlined v-else />
                </a-button>
              </a-space>
            </div>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 表格 -->
    <div class="table-wrapper">
      <AvicTable
        ref="avicTable"
        :bordered="false"
        :columns="columns"
        :row-key="(record) => record.dbId"
        :data-source="list"
        :loading="loading"
        :row-selection="{
          selectedRowKeys: selectedRowKeys,
          onChange: onSelectChange,
          columnWidth: 40,
          fixed: true
        }"
        :custom-row="
          (record) => {
            return {
              onClick: () => {
                handleRowSelection(record);
              },
              onDblclick: () => {
                handleRowSelection(record);
              }
            };
          }
        "
        :page-parameter="queryParam.pageParameter"
        :total="totalPage"
        @change="handleTableChange"
        @refresh="getList"
      >
        <template #bodyCell="{ column, text, record, index }">
          <template v-if="column.key === 'id'">
            {{ index + 1 + queryParam.pageParameter.rows * (queryParam.pageParameter.page - 1) }}
          </template>
          <template v-else-if="column.key === 'taskTitle'">
            <a href="javascript:;" class="table-detail-link" @click.stop="handleDetail(record.dbId)">
              {{ record.title || record.procDefName }}
            </a>
          </template>
          <template v-else-if="column.key === 'businessState'">
            <AvicDictTag
              style="display: inline-block"
              :value="record.processBusinessState"
              match-field="lookupCode"
              :options="businessStateList"
            />
            <a-tag v-if="text === 'suspended'" color="orange" style="margin-left: 5px">已挂起</a-tag>
          </template>
        </template>
      </AvicTable>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { listHistProcessByPage } from '@/api/avic/bpm/BpmTaskApi';
import type { MyDraftQueryForm, treeParams } from '../types';
import bpmUtils from '../bpmutils/FlowUtils';
import BpmInstanceObject from '../bpmutils/FlowEditor';
const { proxy } = getCurrentInstance();
const props = defineProps({
  params: {
    type: Object,
    default: null
  }
});
const $emit = defineEmits(['getMyProcessCount']);
const layout = {
  labelCol: { flex: '0 0 100px' },
  wrapperCol: { flex: '1 1 0' }
};
const colLayout = proxy.$colLayout4; // 页面表单响应式布局对象
const selectType = 'multi'; // 表格单选: single, 多选: multi
const lookupParams = [{ fieldName: 'businessState', lookUpType: 'PLATFORM_BPM_TASK_STATE' }];
const columns = [
  {
    title: '序号',
    ellipsis: true,
    width: 56,
    maxWidth: 56,
    align: 'center',
    key: 'id',
    fixed: 'left'
  },
  {
    title: '流程名称',
    dataIndex: 'procDefName',
    key: 'taskTitle',
    ellipsis: true,
    resizable: true,
    align: 'left',
    minWidth: 320
  },
  {
    title: '当前节点',
    dataIndex: 'activityName',
    align: 'left',
    ellipsis: true,
    resizable: true,
    width: 160,
    minWidth: 160
  },
  {
    title: '状态',
    dataIndex: 'businessState',
    key: 'businessState',
    align: 'left',
    sorter: true,
    resizable: true,
    width: 150
  },
  {
    title: '处理人',
    dataIndex: 'assigneeName',
    ellipsis: true,
    width: 180,
    resizable: true,
    align: 'left'
  },
  {
    title: '创建时间',
    dataIndex: 'startDate',
    width: 180,
    minWidth: 180,
    align: 'left',
    sorter: true,
    resizable: true,
    customRender: (obj) => {
      if (obj.text) {
        return proxy.$dateFormat(obj.text);
      }
      return '';
    }
  },
  {
    title: '结束时间',
    dataIndex: 'endDate',
    width: 180,
    minWidth: 180,
    align: 'left',
    sorter: true,
    resizable: true,
    customRender: (obj) => {
      if (obj.text) {
        return proxy.$dateFormat(obj.text);
      }
      return '';
    }
  }
];
const queryForm = ref<MyDraftQueryForm>({
  title: undefined,
  businessState: undefined,
  startDateBegin: undefined,
  startDateEnd: undefined,
  type: '2'
});
const treeForms = ref<treeParams>({
  nodeId: '',
  nodeType: ''
});

const advanced = ref(false); // 高级搜索 展开/关闭
const queryParam = reactive({
  // 请求表格数据参数
  pageParameter: {
    page: 1, // 页数
    rows: 20 // 每页条数
  },
  searchParams: {
    ...treeForms.value,
    ...queryForm.value
  },
  keyWord: ref(''), // 快速查询数据
  sidx: null, // 排序字段
  sord: null // 排序方式: desc降序 asc升序
});
const list = ref([]); // 表格数据集合
const selectedRowKeys = ref([]); // 选中数据主键集合
const loading = ref<boolean>(false); // 表格loading状态
const totalPage = ref<number>(0);
const businessStateList = ref([]); // 高级查询通用代码字段
const bpmInstanceObject = ref(new BpmInstanceObject());

watch(
  props.params,
  (newVal: treeParams) => {
    treeForms.value = { ...newVal };
    queryParam.searchParams = {
      ...treeForms.value,
      ...queryForm.value
    };
    // 加载表格数据
    if (treeForms.value.nodeId && treeForms.value.nodeId !== '') {
      getList();
    } else {
      list.value = [];
      totalPage.value = 0;
    }
  },
  {
    immediate: true
  }
);

onMounted(() => {
  // 加载查询区所需通用代码
  getLookupList();
});

/** 查询数据  */
function getList() {
  selectedRowKeys.value = []; // 清空选中
  loading.value = true;
  listHistProcessByPage(queryParam)
    .then((response: any) => {
      list.value = response.data.rows;
      totalPage.value = response.data.records;
    })
    .finally(() => {
      loading.value = false;
    })
    .catch(() => {
      list.value = [];
      totalPage.value = 0;
    });
}

/** 获取通用代码  */
function getLookupList() {
  proxy.$getLookupByType(lookupParams, (result) => {
    businessStateList.value = result.businessState;
  });
}
/** 高级查询 查询按钮操作 */
function handleQuery() {
  queryParam.searchParams = {
    ...treeForms.value,
    ...queryForm.value
  };
  queryParam.keyWord = '';
  queryParam.pageParameter.page = 1;
  getList();
}
/** 高级查询 重置按钮操作 */
function resetQuery() {
  queryForm.value = { type: '2' } as MyDraftQueryForm;
  handleQuery();
}
/** 高级查询 展开/收起 */
function toggleAdvanced() {
  advanced.value = !advanced.value;
}
/** 勾选复选框时触发 */
function onSelectChange(_selectedRowKeys: string[]) {
  selectedRowKeys.value = _selectedRowKeys;
}
/** 表格行选中 */
function handleRowSelection(record) {
  const selectIds = [...selectedRowKeys.value];
  if (selectType === 'multi') {
    // 多选
    if (!selectIds.includes(record.dbId)) {
      // 选中
      selectedRowKeys.value.push(record.dbId);
    } else {
      // 取消选中
      selectedRowKeys.value = selectedRowKeys.value.filter((item) => item !== record.dbId);
    }
  } else {
    // 单选
    if (!selectIds.includes(record.dbId)) {
      selectedRowKeys.value = [record.dbId];
    } else {
      selectedRowKeys.value = []; // 单选可取消
    }
  }
}
/** 表格排序 */
function handleTableChange(pagination, _filters, sorter) {
  queryParam.pageParameter.page = pagination.current;
  queryParam.pageParameter.rows = pagination.pageSize;
  if (proxy.$objIsNotBlank(sorter.field)) {
    queryParam.sidx = sorter.field;
    queryParam.sord = sorter.order === 'ascend' ? 'asc' : 'desc'; // 排序方式: desc降序 asc升序
  }
  getList();
}
// 打开流程详情
function handleDetail(dbId: string) {
  bpmUtils.detail(bpmInstanceObject, dbId, '', '', '', bpmOperatorRefresh).catch(() => {
    proxy.$message.error('启动流程失败!');
  });
}
/**
 * 流程详情页面关闭后刷新页面
 */
function bpmOperatorRefresh() {
  if (treeForms.value.nodeId && treeForms.value.nodeType) {
    getList();
    $emit('getMyProcessCount');
  }
}
defineExpose({
  bpmOperatorRefresh
});
</script>
<style lang="less"></style>
